directives.directive('dateFilter', ["$rootScope", "tools", "$timeout", "$routeParams", "tmp", function ($rootScope, tools, $timeout, $routeParams, tmp) {
    return {
        restrict: 'E',
        scope: true,
        replace: true,
        link: function (scope, element, attr) {
            var config = scope.config = scope.getCondition(attr.conditionid ? Number(attr.conditionid) : Number(attr.tmpid));
            var events = {};
            var d = new Date();
            var $ele = $(element);
            var startDate, endDate;
            scope.dates = [];

            var makeDatePicker = function (startDate_, endDate_) {
                scope.dates.length = 0;
                var ele_id = 'dateFilter' + d.getDate() + '-' + d.getHours() + '-' + d.getMinutes() + '-' + d.getSeconds();
                var ele_input_trigger_id = 'input_trigger' + d.getDate() + '-' + d.getHours() + '-' + d.getMinutes() + '-' + d.getSeconds();

                scope.dates.push({
                    'shown': true,
                    'ele_id': ele_id,
                    'ele_input_trigger_id': ele_input_trigger_id
                });

                $timeout(function () {
                    new pickerDateRange(ele_id, {
                        inputTrigger: ele_input_trigger_id,
                        startDate: startDate_,
                        endDate: endDate_,
                        isTodayValid: true,
                        defaultText: ' 至 ',
                        theme: 'ta',
                        success: function (obj) {
                            $rootScope.$broadcast('FILTER_EV', config.conditionId, obj.startDate + ' 00:00:00' + ',' + obj.endDate + ' 00:00:00');
                        }
                    });
                }, 100);
            }

            scope.refresh = function (e) {
                $rootScope.$broadcast('FILTER_EV', config.conditionId, '');
                makeDatePicker(startDate, endDate);
                e.stopPropagation();
            }

            scope.clearCondition = function () {
                $rootScope.$broadcast('FILTER_CLEAR_EV', config.conditionId);
            }

            scope.clearData = function () {
                config.column = null;
                scope.fieldTitle = '';
                scope.recover_fields();
                if (scope.dates[0]) {
                    scope.dates[0].shown = false;
                }
                scope.config.selected = scope.selected = false;
            }

            // 获取数据
            scope.getConditionData = function (conditionId) {
                tools.get({
                    url: '/Ibm-Client/getRangeDatasByConditionId.htm',
                    data: {
                        conditionId: conditionId,
                        pageId: $routeParams.id
                    },
                    succ: function (resp) {
                        if (resp.success) {
                            //resp.value.min = '2016-01-01 13:44:33';
                            //resp.value.max = '2016-07-01 13:44:33';
                            if (!/\d{4}-\d{2}-\d{2}/.test(resp.value.min) || !/\d{4}-\d{2}-\d{2}/.test(resp.value.max)) {
                                return tools.msg("时间格式有误!");
                            }

                            startDate = /\d{4}-\d{2}-\d{2}/.exec(resp.value.min)[0];
                            endDate = /\d{4}-\d{2}-\d{2}/.exec(resp.value.max)[0];

                            makeDatePicker(startDate, endDate);
                            $ele.removeClass(attr.type);
                        }
                    }
                });
            }

            if (config.conditionId) {
                scope.getConditionData(config.conditionId);
            } else {
                $ele.addClass(attr.type);
            }

            // 监听取消预览事件
            events.PREVIEW_OFF_EV_OFF = $rootScope.$on('PREVIEW_OFF_EV', function () {
            });
        },
        templateUrl: "/trend-new-BI/app/components/common/chartManager/date/dateView.html"
    }
}]);